package com.cqut.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cqut.model.CodeManager;
import com.cqut.model.JudgesManager;
import com.cqut.dao.JudgesManagerMapper;
import com.cqut.dao.JudgesManagerMapperExtra;
import com.cqut.dto.CodeManagerDTO;
import com.cqut.dto.JudgesManagerDTO;
import com.cqut.dto.LimitShowDTO;
import com.cqut.service.IJudgesManagerService;
import com.cqut.util.SqlUtil;
import com.cqut.util.StringUtil;

@Service
public class JudgesManagerService implements IJudgesManagerService {
    @Resource(name="judgesManagerMapper")
    private JudgesManagerMapper judgesManagerMapper;
    @Resource(name = "judgesManagerMapperExtra")
    private JudgesManagerMapperExtra judgesManagerMapperExtra;
    @Override
    public boolean save(JudgesManager judgesManager) {
    	String judgesId = String.valueOf(System.currentTimeMillis());
    	//新建默认为待审核
    	int judgesState = 2;
    	//初始化密码默认为123456
    	String loadPassword = "123456";
    	//评委的角色ID设为0003
    	String roleid = "0003";
    	judgesManager.setRoleID(roleid);
    	judgesManager.setJudgesId(judgesId);
    	judgesManager.setJudgesState(judgesState);
    	judgesManager.setLoadPassword(loadPassword);
        return judgesManagerMapper.insert(judgesManager) > 0;
    }

    @Override
    public boolean update(JudgesManager judgesManager) {
        return judgesManagerMapper.updateByPrimaryKeySelective(judgesManager) >= 0;
    }

    @Override
    public boolean delete(String judgesId) {
        return judgesManagerMapper.deleteByPrimaryKey(judgesId) > 0;
    }

    @Override
    public boolean batchDelete(String[] primaryKeys) {
        return judgesManagerMapper.deleteByPrimaryKeys(primaryKeys) > 0;
    }

    @Override
    public JudgesManager findModel(String judgesId) {
        return judgesManagerMapper.selectByPrimaryKey(judgesId);
    }

    @Override
    public JudgesManager findModel(String judgesId, String[] parameters) {
        return judgesManagerMapper.findModel(judgesId, SqlUtil.formatParameters(parameters));
    }

    public Map<String, Object> findMap(String judgesId) {
        String[][] parameters = {{"judges_id", "judgesId"}, {"judges_name", "judgesName"}, {"load_password", "loadPassword"}, {"mailbox", "mailbox"}, {"education", "education"}, {"title", "title"}, {"research_field", "researchField"}, {"subordinate_units", "subordinateUnits"}, {"judges_state", "judgesState"}, {"adder_id", "adderId"}, {"add_name", "addName"}, {"add_time", "addTime"},{"role_id", "roleid"},{"judges_phone", "judgesPhone"}};
        return this.findMap(judgesId, parameters);
    }

    public Map<String, Object> findMap(String judgesId, String[][] parameters) {
        return judgesManagerMapper.findMap(judgesId, SqlUtil.formatParametersForAlias(parameters));
    }

    @Override
    public List<JudgesManager> loadModels() {
        return this.loadModels(null, null, null, null, -1, -1);
    }

    @Override
    public List<JudgesManager> loadModels(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return this.loadModels(null, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<JudgesManager> loadModels(String[] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return judgesManagerMapper.loadModels(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps() {
        String[][] parameters = {{"judges_id", "judgesId"}, {"judges_name", "judgesName"}, {"load_password", "loadPassword"}, {"mailbox", "mailbox"}, {"education", "education"}, {"title", "title"}, {"research_field", "researchField"}, {"subordinate_units", "subordinateUnits"}, {"judges_state", "judgesState"}, {"adder_id", "adderId"}, {"add_name", "addName"}, {"add_time", "addTime"}};
        return this.loadMaps(parameters, null, null, null, null, -1, -1);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String condition, Object[] values, String order, String sort, int curPage, int limit) {
        String[][] parameters = {{"judges_id", "judgesId"}, {"judges_name", "judgesName"}, {"load_password", "loadPassword"}, {"mailbox", "mailbox"}, {"education", "education"}, {"title", "title"}, {"research_field", "researchField"}, {"subordinate_units", "subordinateUnits"}, {"judges_state", "judgesState"}, {"adder_id", "adderId"}, {"add_name", "addName"}, {"add_time", "addTime"},{"role_id", "roleid"},{"judges_phone", "judgesPhone"}};
        return this.loadMaps(parameters, condition, values, order, sort, curPage, limit);
    }

    @Override
    public List<Map<String, Object>> loadMaps(String[][] parameters, String condition, Object[] values, String order, String sort, int curPage, int limit) {
        return judgesManagerMapper.loadMaps(SqlUtil.formatParametersForAlias(parameters), SqlUtil.fillCondition(condition, values), order, sort, SqlUtil.getOffset(curPage, limit), limit);
    }

    @Override
    public long count() {
        return this.count(null, false);
    }

    @Override
    public long count(String condition, Object[] values) {
        return this.count(null, condition, values, false);
    }

    @Override
    public long count(String[] parameters, boolean isDistinct) {
        return this.count(parameters, null, null, isDistinct);
    }

    @Override
    public long count(String[] parameters, String condition, Object[] values, boolean isDistinct) {
        return judgesManagerMapper.count(SqlUtil.formatParameters(parameters), SqlUtil.fillCondition(condition, values), isDistinct);
    }

	@Override
	public List<Map<String, Object>> getEducationList() {
		return judgesManagerMapper.getEducationList();
	}

	@Override
	public List<Map<String, Object>> getTitle() {
		return judgesManagerMapper.getTitle();
	}

	@Override
	public List<Map<String, Object>> findJudgesWithLimit(String judgesName, LimitShowDTO limitShowDTO) {
		// TODO Auto-generated method stub
		return judgesManagerMapperExtra.findJudgesByLimit(judgesName, limitShowDTO.calculateStart(), limitShowDTO.calculateEnd());
	}

	@Override
	public long findCountOfJudges(String judgesName) {
		// TODO Auto-generated method stub
		long count = judgesManagerMapperExtra.findCountByLimit(judgesName);
		return count;
	}


}
